<?php
	require_once('globals.php');
	$notlogin = true;
	
	require_once('inc/weixiconfig.php');
	$xml = file_get_contents('php://input');
	$xml_tree = new DOMDocument();
	$xml_tree->loadXML($xml);
	$paystate=1;
	
	if($xml_tree->getElementsByTagName('return_code')->item(0)->nodeValue=='SUCCESS'){
		if(checksign($xml_tree)){
			if($xml_tree->getElementsByTagName('result_code')->item(0)->nodeValue=='SUCCESS'){
				$out_trade_no	= $xml_tree->getElementsByTagName('out_trade_no')->item(0)->nodeValue;	    //获取订单号
				$total_fee		= $xml_tree->getElementsByTagName('total_fee')->item(0)->nodeValue;			//获取总价格
				
				$or_info = $db->get('tb_order', '*', array('order_number'=>$out_trade_no));
				
				if($or_info && $or_info['status']==1){
				    $db->update('tb_order', array('status'=>2,'paytime'=>date("Y-m-d H:i:s")), array('order_number'=>$out_trade_no));
				    
				    //log
				    $db->insert('tb_order_log', array('order_no'=>$out_trade_no, 'amount'=>$total_fee, 'dt'=>date("Y-m-d H:i:s")));
				}else{
				    $paystate=5;
				}
				
			}else{
				$paystate=4;
			}
		}else{
			$paystate=3;
		}
	}else{
		$paystate=2;
	}
	//$db->insert('trade',array('test'=>$xml,'tradenumber'=>1,'money'=>2,'#datetime'=>'now()','member_id'=>1));
	if($paystate==1){
		echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
	}else{
		echo "<xml><return_code><![CDATA[FAIL]]></return_code></xml>";
	}
?>
